package com.example.demo4

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    
    companion object {
        const val REQUEST_CODE_EDIT = 1001
        const val EXTRA_NAME = "extra_name"
        const val EXTRA_GENDER = "extra_gender"
        const val EXTRA_STUDENT_ID = "extra_student_id"
        const val EXTRA_PHONE = "extra_phone"
        const val EXTRA_EMAIL = "extra_email"
        const val EXTRA_HOBBIES = "extra_hobbies"
    }
    
    private lateinit var tvName: TextView
    private lateinit var tvGender: TextView
    private lateinit var tvStudentId: TextView
    private lateinit var tvPhone: TextView
    private lateinit var tvEmail: TextView
    private lateinit var tvHobbies: TextView
    private lateinit var tvHighScore: TextView
    private lateinit var btnEditInfo: Button
    private lateinit var btnCardGame: Button
    
    private var highScore = 0
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        initViews()
        setupListeners()
    }
    
    private fun initViews() {
        tvName = findViewById(R.id.tvName)
        tvGender = findViewById(R.id.tvGender)
        tvStudentId = findViewById(R.id.tvStudentId)
        tvPhone = findViewById(R.id.tvPhone)
        tvEmail = findViewById(R.id.tvEmail)
        tvHobbies = findViewById(R.id.tvHobbies)
        tvHighScore = findViewById(R.id.tvHighScore)
        btnEditInfo = findViewById(R.id.btnEditInfo)
        btnCardGame = findViewById(R.id.btnCardGame)
    }
    
    private fun setupListeners() {
        btnEditInfo.setOnClickListener {
            val intent = Intent(this, PersonInformationActivity::class.java).apply {
                putExtra(EXTRA_NAME, tvName.text.toString())
                putExtra(EXTRA_GENDER, tvGender.text.toString())
                putExtra(EXTRA_STUDENT_ID, tvStudentId.text.toString())
                putExtra(EXTRA_PHONE, tvPhone.text.toString())
                putExtra(EXTRA_EMAIL, tvEmail.text.toString())
                putExtra(EXTRA_HOBBIES, tvHobbies.text.toString())
            }
            startActivityForResult(intent, REQUEST_CODE_EDIT)
        }
        
        btnCardGame.setOnClickListener {
            val intent = Intent(this, CardGameActivity::class.java)
            startActivityForResult(intent, REQUEST_CODE_EDIT)
        }
    }
    
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        
        if (requestCode == REQUEST_CODE_EDIT && resultCode == RESULT_OK && data != null) {
            // 更新个人信息
            data.getStringExtra(EXTRA_NAME)?.let { tvName.text = it }
            data.getStringExtra(EXTRA_GENDER)?.let { tvGender.text = it }
            data.getStringExtra(EXTRA_STUDENT_ID)?.let { tvStudentId.text = it }
            data.getStringExtra(EXTRA_PHONE)?.let { tvPhone.text = it }
            data.getStringExtra(EXTRA_EMAIL)?.let { tvEmail.text = it }
            data.getStringExtra(EXTRA_HOBBIES)?.let { tvHobbies.text = it }
            
            // 更新游戏最高分
            val score = data.getIntExtra("score", -1)
            if (score > highScore) {
                highScore = score
                tvHighScore.text = highScore.toString()
            }
        }
    }
}